﻿using MediatR;
using Medical.Api.Read.Application.Command.Wms.WmsStorageCommand;
using Medical.Api.Read.Dto.WmsDto.WmsStorageDto;
using Medical.Domain.Wms;
using Medical.ErrorCode;
using Medical.Interstructrue.Base;

namespace Medical.Api.Read.Application.Handler.Wms.WmsStorageHandler
{
    /// <summary>
    /// 入库详情查询处理器，根据入库单id获取主表及明细
    /// </summary>
    public class GetStorageByIdHandler : IRequestHandler<GetStorageByIdCommand, ApiResult<WmsStorageDetailDto>>
    {
        /// <summary>
        /// 入库主表仓储
        /// </summary>
        private readonly IBaseRepository<WmsStorage> wmsStorageRep;
        /// <summary>
        /// 入库明细表仓储
        /// </summary>
        private readonly IBaseRepository<WmsStorageDetail> wmsStorageDetailRep;
        /// <summary>
        /// 构造函数，注入主表和明细表仓储
        /// </summary>
        public GetStorageByIdHandler(IBaseRepository<WmsStorage> wmsStorageRep, IBaseRepository<WmsStorageDetail> wmsStorageDetailRep)
        {
            this.wmsStorageRep = wmsStorageRep;
            this.wmsStorageDetailRep = wmsStorageDetailRep;
        }
        /// <summary>
        /// 查询入库详情及明细
        /// </summary>
        /// <param name="request">入库单id命令</param>
        /// <param name="cancellationToken"></param>
        /// <returns>入库详情及明细</returns>
        public Task<ApiResult<WmsStorageDetailDto>> Handle(GetStorageByIdCommand request, CancellationToken cancellationToken)
        {
            var result = new ApiResult<WmsStorageDetailDto>();
            try
            {
                // 查询主表
                var storage = wmsStorageRep.GetAll().FirstOrDefault(x => x.Id == request.Id && !x.IsDeleted);
                if (storage == null)
                {
                    result.Code = ApiEnums.NotFound;
                    result.Msg = $"未找到Id为{request.Id}的入库记录";
                    result.Data = null;
                    return Task.FromResult(result);
                }
                // 查询明细
                var details = wmsStorageDetailRep.GetAll().Where(d => d.StorageId == request.Id && !d.IsDeleted).ToList();
                // 组装DTO
                var dto = new WmsStorageDetailDto
                {
                    Id = storage.Id,
                    StorageCode = storage.StorageCode,
                    StorageType = (int)storage.StorageType,
                    StorageTypeName = storage.StorageType.ToString(),
                    Warehouse = (int)storage.Warehouse,
                    WarehouseName = storage.Warehouse.ToString(),
                    Supplier = (int)storage.Supplier,
                    SupplierName = storage.Supplier.ToString(),
                    StorageExplain = storage.StorageExplain,
                    AuditState = storage.AuditState,
                    AuditName = storage.AuditName,
                    AuditDate = storage.AuditDate,
                    CreateTime = storage.CreateTime,
                    Details = details.Select(d => new WmsStorageDetailItemDto
                    {
                        Id = d.Id,
                        MaterialId = d.MaterialId,
                        MaterialType = (int)d.MaterialType,
                        MaterialTypeName = d.MaterialType.ToString(),
                        MaterialCode = d.MaterialCode,
                        MaterialName = d.MaterialName,
                        StorageNum = d.StorageNum,
                        MaterialSpecs = d.MaterialSpecs,
                        MaterialPrice = d.MaterialPrice,
                        RegisterNo = d.RegisterNo,
                        ProduceDate = d.ProduceDate,
                        SterilizaDate = d.SterilizaDate,
                        LapseDate = d.LapseDate
                    }).ToList()
                };
                result.Code = ApiEnums.Success;
                result.Msg = "查询成功";
                result.Data = dto;
            }
            catch (Exception ex)
            {
                result.Code = ApiEnums.Error;
                result.Msg = $"查询异常: {ex.Message}";
                result.Data = null;
            }
            return Task.FromResult(result);
        }
    }
}
